﻿/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using Bio.Data.Providers.Interfaces;
using System.IO;
using Bio.Data.Interfaces;
using System.Diagnostics;

namespace Bio.Data.Providers
{
    /// <summary>
    /// Base class used for any file-based data loaders.
    /// </summary>
    /// <typeparam name="T">Type of data held in file</typeparam>
    public abstract class BioFileDataLoader<T> : IBioDataLoader<T>, IDisposable
    {
        private List<T> _entityList = new List<T>();

        /// <summary>
        /// This provides access to any initialization data used to create this loader.
        /// </summary>
        public string InitializationData { get; protected set; }

        /// <summary>
        /// The list of entities
        /// </summary>
        public IList<T> Entities { get { return _entityList; } }

        /// <summary>
        /// The list of entities
        /// </summary>
        System.Collections.IList IBioDataLoader.Entities
        {
            get { return (System.Collections.IList)_entityList; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public BioFileDataLoader()
        {
            IsReadOnly = true;
        }

        /// <summary>
        /// This is used to initialize the BioDataLoader when it is first created.
        /// </summary>
        /// <param name="initData">String data</param>
        /// <returns>True/False success</returns>
        public bool Initialize(string initData)
        {
            InitializationData = initData;
            return OnInitialize();
        }

        /// <summary>
        /// Method that initializes the data provider.
        /// </summary>
        /// <returns>True/False success indicator</returns>
        protected virtual bool OnInitialize()
        {
            return File.Exists(InitializationData);
        }

        /// <summary>
        /// This method is used to prepare the loader to access the
        /// Entities collection.
        /// </summary>
        /// <returns>Count of loaded records</returns>
        public int Load()
        {
            if (!string.IsNullOrEmpty(InitializationData))
            {
                Entities.Clear();
                OnLoad();
                return Entities.Count;
            }

            return 0;
        }

        /// <summary>
        /// Method that loads the data.
        /// </summary>
        protected virtual void OnLoad()
        {
        }

        /// <summary>
        /// String that describes the data (filename, filter, etc.)
        /// </summary>
        public string DataDescription { get { return Path.GetFileName(InitializationData); } }

        /// <summary>
        /// Returns whether this loader supports R/W capabilities.
        /// </summary>
        public bool IsReadOnly { get; protected set; }

        /// <summary>
        /// Used to dispose the provider
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Method used to get rid of unmanaged resources.
        /// </summary>
        /// <param name="isDisposing">True if disposing</param>
        protected virtual void Dispose(bool isDisposing)
        {
        }
    }
}
